/*
 * Package il.ac.biu.cs.grossmm.test.cppToFooAdapter
 * File AuthorizationManager.java
 * 
 * This is a part of presence service framework API. 
 * See javadoc for more information.
 *  
 * Copyright (C) 2006 Grossmann Mark
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */
package il.ac.biu.cs.grossmm.tests.fooAdapters;

import il.ac.biu.cs.grossmm.api.OperationFailedException;
import il.ac.biu.cs.grossmm.api.presence.Entity;
import il.ac.biu.cs.grossmm.api.presence.EntityType;
import il.ac.biu.cs.grossmm.api.presence.Registrar;
import il.ac.biu.cs.grossmm.api.server.Component;
import il.ac.biu.cs.grossmm.api.server.ComponentManager;
import il.ac.biu.cs.grossmm.api.server.ServiceManager;
import il.ac.biu.cs.grossmm.api.server.Service;

import java.util.AbstractSet;
import java.util.Iterator;
import java.util.Set;
import java.util.WeakHashMap;

public class Authenticator implements Service, Component, AuthenticationHandler, Registrar {
	Registrar registrar;
	FooProvider provider;
	
	WeakHashMap<String, DummyEntity> ents = new WeakHashMap<String, DummyEntity>();
	
	
	class DummyEntity extends AbstractSet<String> implements Entity {
		final String identifier;
		
		
		DummyEntity(String ident) {
			this.identifier = ident;
			 
		}

		public Set<String> getIdentifiers()
		{
			return this;
		}

		public <T> T getProperty(String property) throws OperationFailedException
		{
			return null;
		}

		public boolean is(EntityType entityType)
		{
			return true;
		}

		public boolean isPermanent()
		{
			return false;
		}

		public boolean isValid()
		{
			return true;
		}

		@Override
		public Iterator<String> iterator()
		{
			return new Iterator<String>() {
				boolean b = false;
				public boolean hasNext()
				{
					if( b )
						return false;
					
					b = true;
					
					return true;
				}

				public String next()
				{
					return identifier;
				}

				public void remove()
				{

					throw new RuntimeException("Invalid op");
				}
				
			};
		}

		@Override
		public int size()
		{
			return 1;
		}
		
	}

	public Entity authenticate(String user, String password)
	{
		try {
//			 return registrar.getEntity(user);
			 return getEntity(user);
			
		} catch (OperationFailedException e) {
			return null;
		}
	}

	public void aborted(Object hint, Exception e) throws Exception
	{
		throw(e);
	}

	public void setComponentManager(ComponentManager container) throws Exception
	{
		registrar = container.getByClass(Registrar.class);
		provider = container.getByClass(FooProvider.class);
		
		provider.registerAuthenticationHandler(this);
	}

	public void start(ServiceManager serviceManager) throws Exception
	{
		System.out.println("Authenticator started");
		
//		System.out.println("Registering bobs");
//		
//		for( int i=0; i<1000000; i++ ) {
//			
//			String bob = "Bob"+i;
//			if( registrar.getEntity(bob) == null ) {
//				 
//				Entity e = registrar.registerEntity(bob, EntityType.PRESENTITY);
//				registrar.registerRole(e, EntityType.WATCHER);
//				
//				// System.out.println("Already in base "+bob);
//			}
//			
//			if( i%100 == 0 )
//				System.out.println(".."+i);
//		}
//		
//		System.out.println("Registered bobs");
	}

	public void stop(Exception exception)
	{
	}

	public void terminated(Object serviceHint, Exception e) throws Exception
	{
		throw(e);
	}

	public void deregister(Entity entity) throws OperationFailedException
	{
	}

	public boolean deregisterAlias(String identifier) throws OperationFailedException
	{
		return false; 
	}

	public boolean deregisterRole(Entity entity, EntityType entityType) throws OperationFailedException
	{
		return false;
	}

	public Entity getEntity(String user) throws OperationFailedException
	{
		//return new DummyEntity(identifier);
		DummyEntity e = ents.get(user);
		if( e == null ) {
			e = new DummyEntity(user);
			ents.put(user, e);
		}
		
		return e;
	}

	public <T> T getProperty(Entity entity, String property) throws OperationFailedException
	{
		return null;
	}

	public void registerAlias(Entity entity, String identifier) throws OperationFailedException
	{
	}

	public Entity registerEntity(String identifier, EntityType entityType) throws OperationFailedException
	{
		return new DummyEntity(identifier);
	}

	public void registerRole(Entity entity, EntityType entityType) throws OperationFailedException
	{
	}

	public void removeProperty(Entity entity, String property) throws OperationFailedException
	{
	}

	public void setProperty(Entity entity, String property, Object value) throws OperationFailedException
	{
	}

}
